package com.ruoyi.collect.controller;

import com.ruoyi.collect.domain.CollectSite;
import com.ruoyi.collect.service.ICollectSiteService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 采集站点控制器
 *
 * @author ruoyi
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/collect/site")
public class CollectSiteController extends BaseController {

    @Autowired
    private ICollectSiteService collectSiteService;

    /**
     * 查询采集站点列表
     */
    @PreAuthorize("@ss.hasPermi('collect:site:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollectSite collectSite) {
        startPage();
        List<CollectSite> list = collectSiteService.selectCollectSiteList(collectSite);
        return getDataTable(list);
    }

    /**
     * 导出采集站点列表
     */
    @PreAuthorize("@ss.hasPermi('collect:site:export')")
    @Log(title = "采集站点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollectSite collectSite) {
        List<CollectSite> list = collectSiteService.selectCollectSiteList(collectSite);
        ExcelUtil<CollectSite> util = new ExcelUtil<CollectSite>(CollectSite.class);
        util.exportExcel(response, list, "采集站点数据");
    }

    /**
     * 获取采集站点详细信息
     */
    @PreAuthorize("@ss.hasPermi('collect:site:query')")
    @GetMapping(value = "/{siteId}")
    public AjaxResult getInfo(@PathVariable("siteId") Long siteId) {
        return AjaxResult.success(collectSiteService.selectCollectSiteById(siteId));
    }

    /**
     * 新增采集站点
     */
    @PreAuthorize("@ss.hasPermi('collect:site:add')")
    @Log(title = "采集站点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CollectSite collectSite) {
        return toAjax(collectSiteService.insertCollectSite(collectSite));
    }

    /**
     * 修改采集站点
     */
    @PreAuthorize("@ss.hasPermi('collect:site:edit')")
    @Log(title = "采集站点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollectSite collectSite) {
        return toAjax(collectSiteService.updateCollectSite(collectSite));
    }

    /**
     * 删除采集站点
     */
    @PreAuthorize("@ss.hasPermi('collect:site:remove')")
    @Log(title = "采集站点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{siteIds}")
    public AjaxResult remove(@PathVariable Long[] siteIds) {
        return toAjax(collectSiteService.deleteCollectSiteByIds(siteIds));
    }

    /**
     * 测试站点连接
     */
    @PreAuthorize("@ss.hasPermi('collect:site:test')")
    @GetMapping("/test/{siteId}")
    public AjaxResult testConnection(@PathVariable Long siteId) {
        boolean result = collectSiteService.testSiteConnection(siteId);
        return result ? AjaxResult.success("连接成功")
                      : AjaxResult.error("连接失败");
    }

    /**
     * 获取启用的站点列表
     */
    @GetMapping("/active")
    public AjaxResult getActiveSites() {
        List<CollectSite> sites = collectSiteService.selectActiveSites();
        return AjaxResult.success(sites);
    }

    /**
     * 获取站点采集数据统计
     */
    @PreAuthorize("@ss.hasPermi('collect:site:query')")
    @GetMapping("/data/stat/{siteId}")
    public AjaxResult getDataStats(@PathVariable Long siteId) {
        java.util.Map<String, Object> stats = collectSiteService.getCollectedDataStats(siteId);
        return AjaxResult.success(stats);
    }

    /**
     * 删除站点的所有采集数据
     */
    @PreAuthorize("@ss.hasPermi('collect:site:remove')")
    @Log(title = "删除采集数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/data/{siteId}")
    public AjaxResult deleteCollectedData(@PathVariable Long siteId) {
        java.util.Map<String, Object> result = collectSiteService.deleteCollectedData(siteId);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return AjaxResult.success((String) result.get("message"), result);
        } else {
            return AjaxResult.error((String) result.get("message"));
        }
    }
}
